package org.wwb.antbox.user.service.userrole;

import org.ant.commons.commons.log.Log;
import org.ant.commons.commons.log.LogFactory;
import org.ant.commons.models.result.BaseResult;
import org.ant.mybatis.query.Query;
import org.ant.service.common.BaseService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.wwb.antbox.user.data.userrole.SysUserRoleMapper;
import org.wwb.antbox.user.iservice.userrole.ISysUserRoleService;
import org.wwb.antbox.user.model.userrole.entity.SysUserRole;
import org.wwb.antbox.user.model.userrole.model.SysUserRoleAddModel;
import org.wwb.antbox.user.model.userrole.model.SysUserRoleDelModel;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author Bond
 * @Date: 2020/2/23 10:51
 * @Description: 用户角色关系Service实现
 */
@Service(registry = {"registryConfigUser"},provider ="dubboProviderUser")
public class SysUserRoleServiceImpl extends BaseService<SysUserRoleMapper, SysUserRole, String> implements ISysUserRoleService {

    private static final Log log = LogFactory.getLog(SysUserRoleServiceImpl.class);

    @Autowired
    private ISysUserRoleService sysUserRoleService;

    @Override
    public BaseResult<List<String>> getSysUserRoleIdList(String userId) {
        Query query = new Query().eq("user_id",userId);
        List<SysUserRole> sysUserRoleList = mapper.list(query);

        List<String> returnModel = new ArrayList<>(sysUserRoleList.size());
        for (SysUserRole sysUserRole : sysUserRoleList){
            returnModel.add(sysUserRole.getRoleId());
        }

        BaseResult<List<String>> baseListResult = new BaseResult<>();
        baseListResult.setModel(returnModel);
        baseListResult.setSuccess(true);
        return baseListResult;
    }

    @Override
    public BaseResult<Integer> saveSysUserRole(SysUserRoleAddModel sysUserRoleAddModel) {
        /**
         * 1、先删除用户角色关系
         */
        SysUserRoleDelModel sysUserRoleDelModel = new SysUserRoleDelModel();
        String[] userIds = {sysUserRoleAddModel.getUserId()};
        sysUserRoleDelModel.setUserIds(userIds);
        sysUserRoleService.delSysUserRole(sysUserRoleDelModel);


        /**
         * 2、再添加用户角色关系
         */
        List<String> roleIds = sysUserRoleAddModel.getRoleIds();
        List<SysUserRole> sysUserRoleList = new ArrayList<>(roleIds.size());

        for (String roleId : roleIds){
            SysUserRole sysUserRole = new SysUserRole();
            sysUserRole.setUserId(sysUserRoleAddModel.getUserId());
            sysUserRole.setRoleId(roleId);
            sysUserRoleList.add(sysUserRole);
        }

        int i = 0;
        if(!sysUserRoleList.isEmpty()){
            i = mapper.saveBatch(sysUserRoleList);
        }

        BaseResult<Integer> baseResult = new BaseResult<>();
        baseResult.setSuccess(true);
        baseResult.setModel(i);
        return baseResult;
    }

    @Override
    public void delSysUserRole(SysUserRoleDelModel sysUserRoleDelModel) {
        Query query = new Query().in("user_id",sysUserRoleDelModel.getUserIds());
        mapper.deleteByQuery(query);
    }
}
